package net.betou.trade.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.page.SimplePage;
import net.betou.common.util.TradeUtils;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Log;
import net.betou.trade.main.manager.LogMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 日志管理Action
 * 
 * 日志管理设定为，不可删除今天的日志， 这就需要提取日志的时间，和今天的相互比对
 * 这样在性能上会存在问题。所以，应该在页面上做手脚
 * @author 叶装装
 * 
 */
@Controller
public class LogAct {

	private static final Logger log = LoggerFactory.getLogger( LogAct.class );

	@RequestMapping( "/log/v_list_operating.jspx" )
	public String listOperating( Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		log( request, Log.OPERATING, pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, model );
		return "log/list_operating";
	}

	@RequestMapping( "/log/v_list_login_success.jspx" )
	public String listLoginSuccess( Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		log( request, Log.LOGIN_SUCCESS, pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, model );

		return "log/list_login_success";
	}

	@RequestMapping( "/log/v_list_login_failure.jspx" )
	public String listLoginFailure( Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		log( request, Log.LOGIN_FAILURE, pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, model );
		return "log/list_login_failure";
	}

	@RequestMapping( "/log/o_delete_operating.jspx" )
	public void deleteOperating( Integer [ ] ids, HttpServletResponse response, ModelMap model ) {
		ResponseUtil util = validateDelete( ids, response );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}
		Log [ ] beans = manager.deleteByIds( ids );
		for ( Log bean : beans ){
			log.info( "delete Log id={}", bean.getId( ) );
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );

		return;
	}

	@RequestMapping( "/log/o_delete_operating_batch.jspx" )
	public String deleteOperatingBatch( Integer days, Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model  ) {
		
		if( days == null ){
			days = 0;
		}
		manager.deleteBatch( Log.OPERATING, days );
		model.addAttribute( "message", Messages.DATA_DELETE_SUCCESS );
		return this.listOperating( pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, request, response, model );
	}

	@RequestMapping( "/log/o_delete_login_success.jspx" )
	public void deleteLoginSuccess( Integer [ ] ids, HttpServletResponse response, ModelMap model ) {
		ResponseUtil util = validateDelete( ids, response );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}
		Log [ ] beans = manager.deleteByIds( ids );
		for ( Log bean : beans ){
			log.info( "delete Log id={}", bean.getId( ) );
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );

		return;
	}

	@RequestMapping( "/log/o_delete_login_success_batch.jspx" )
	public String deleteLoginSuccessBatch( Integer days, Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		if( days == null ){
			days = 0;
		}
		manager.deleteBatch( Log.LOGIN_SUCCESS, days );
		model.addAttribute( "message", Messages.DATA_DELETE_SUCCESS );
		return listLoginSuccess( pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, request, response, model );
		
	}

	@RequestMapping( "/log/o_delete_login_failure.jspx" )
	public void deleteLoginFailure( Integer [ ] ids, HttpServletResponse response, ModelMap model ) {

		ResponseUtil util = validateDelete( ids, response );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}
		Log [ ] beans = manager.deleteByIds( ids );
		for ( Log bean : beans ){
			log.info( "delete Log id={}", bean.getId( ) );
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );

		return;
	}

	@RequestMapping( "/log/o_delete_login_failure_batch.jspx" )
	public String deleteLoginFailureBatch(  Integer days, Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		if( days == null ){
			days = 0;
		}
		manager.deleteBatch( Log.LOGIN_FAILURE, days );
		model.addAttribute( "message", Messages.DATA_DELETE_SUCCESS );
		return this.listLoginFailure( pageSize, pageNo, filterStatus, searchProperty, searchValue, orderProperty, orderDirection, request, response, model );
		
	}

	private ResponseUtil validateDelete( Integer [ ] ids, HttpServletResponse response ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}

		return util;
	}

	private void log( HttpServletRequest request, int category, Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, ModelMap model ) {

		pageNo = SimplePage.cpn( pageNo );
		pageSize = SimplePage.cpnPageSize( pageSize );

		Pagination pagination = manager.getPage( category, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		model.addAttribute( "pagination", pagination );
		
		model.addAttribute( "pageSize", pageSize );
		model.addAttribute( "orderDirection", orderDirection );
		model.addAttribute( "orderProperty", orderProperty );
		model.addAttribute( "searchProperty", searchProperty );
		model.addAttribute( "searchValue", searchValue );
		model.addAttribute( "filterStatus", filterStatus );
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		model.addAttribute( "admin", admin );

	}

	@Autowired
	private LogMng manager;
	
}
